/**
 * A4纸张计算
 * 为啥用285mm不用297mm？因为是国内印刷厂最节省的做法
 */
export function A4OriginFormat(direction = 'Vertical') {
  const A4OriginMM = {
    width: direction === 'Vertical' ? 210 : 285,
    height: direction === 'Vertical' ? 285 : 210
  }
  // 210mm: 793.69 297mm: 1122.52 285mm: 1077.16
  const A4OriginPX = {
    width: direction === 'Vertical' ? 793.69 : 1077.16,
    height: direction === 'Vertical' ? 1077.16 : 793.69
  };
  const A4OriginPX300DPI = {
    width: direction === 'Vertical' ? 2480 : 3366,
    height: direction === 'Vertical' ? 3366 : 2480
  }
  const ratio = {
    width: A4OriginPX300DPI.width / A4OriginPX.width,
    height: A4OriginPX300DPI.height / A4OriginPX.height
  }
  return {
    A4OriginMM,
    A4OriginPX,
    A4OriginPX300DPI,
    ratio
  }
}

/**
  * 二进制文件标志符转普通文件
  * @param blobUri 二进制文件标志符
  * @param fileName 文件名
  * @returns File
  */
export function blobUriToFile(blob, fileName) {
  return new File([blob], fileName, { type: 'application/pdf' })
}

/**
 * 打印分辨率类
 */
export class Dpi {
  getDPI() {
    const arrDPI = [];
    if (window.screen.deviceXDPI) {
      arrDPI[0] = window.screen.deviceXDPI;
      arrDPI[1] = window.screen.deviceYDPI;
    } else {
      const tmpNode = document.createElement('DIV');
      tmpNode.style.cssText = 'width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden';
      document.body.appendChild(tmpNode);
      arrDPI[0] = parseInt(tmpNode.offsetWidth);
      arrDPI[1] = parseInt(tmpNode.offsetHeight);
      tmpNode.parentNode.removeChild(tmpNode);
    }
    return arrDPI;
  }
  /**
   * px转换为mm
   * @param value
   * @returns {number}
   */
  pxToMm(value) {
    const inch = value / this.getDPI()[0];
    const c_value = inch * 25.4;
    return c_value;
  }
  /**
   * mm转换为px
   * @param value
   * @returns {number}
   */
  mmToPx(value) {
    const inch = value / 25.4;
    const c_value = inch * this.getDPI()[0];
    return c_value;
  }
}

/**
 * @description 浏览器类型验证
 */
export function isBrower() {
  let isFirFox = false;
  if (navigator.userAgent.indexOf('Firefox') !== -1) {
    isFirFox = true
  } else {
    isFirFox = false
  }
  return isFirFox
}
